/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.giant.bean;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.faces.bean.RequestScoped;

import java.io.Serializable;
import java.net.URL;
import java.net.URLEncoder;
import javax.faces.bean.ManagedBean;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;
import org.primefaces.context.RequestContext;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

/**
 *
 * @author Harold
 */
@ManagedBean
@RequestScoped
public final class ReporteController implements Serializable {

    /**
     *
     */
    private StreamedContent file;

    /**
     *
     * @throws IOException
     */
    public ReporteController() throws IOException {
    }

    public void showReport2() {
        RequestContext context = RequestContext.getCurrentInstance();
        context.execute("window.open('http://localhost:8090/jasperserver/rest_v2/reports/reports/test.pdf?j_username=jasperadmin&j_password=jasperadmin','')");
    }

    /**
     * Imprimir reporte
     *
     * @return
     * @throws IOException
     */
    public StreamedContent getFile(String report, String parameters, String module, String downloadName) {
        try {
            if (!parameters.equals("")) {
                parameters=parameters.replace(" ", "%20");
                parameters = "&" + parameters;
            }

            String redirectURL = "http://localhost:8090/jasperserver/rest_v2/reports/reports/" + module + "/" + report + ".pdf?j_username=jasperadmin&j_password=jasperadmin" + parameters;
            System.out.println("ret"+redirectURL);
            // Get the FacesContext
            FacesContext facesContext = FacesContext.getCurrentInstance();

            // Get HTTP response
            HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();

            // Set response headers
            response.reset();   // Reset the response in the first place
            response.setHeader("Content-Type", "application/pdf");  // Set only the content type

            // Open response output stream
            OutputStream responseOutputStream = response.getOutputStream();

            // Read PDF 
            System.out.println(redirectURL);
            URL url = new URL(redirectURL);
            InputStream pdfInputStream = url.openStream();
            file = new DefaultStreamedContent(pdfInputStream, "application/pdf", downloadName + ".pdf");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return file;
    }
//    public StreamedContent submit() throws IOException {
//        String redirectURL = "http://localhost:8090/jasperserver/rest_v2/reports/reports/test.pdf?j_username=jasperadmin&j_password=jasperadmin";
//        // Get the FacesContext
//        FacesContext facesContext = FacesContext.getCurrentInstance();
//         
//        // Get HTTP response
//        HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
//         
//        // Set response headers
//        response.reset();   // Reset the response in the first place
//        response.setHeader("Content-Type", "application/pdf");  // Set only the content type
//         
//        // Open response output stream
//        OutputStream responseOutputStream = response.getOutputStream();
//         
//        // Read PDF contents
//        URL url = new URL(redirectURL);
//        InputStream pdfInputStream = url.openStream();
//         file = new DefaultStreamedContent(pdfInputStream, "image/jpg", "home.png"); 
////        // Read PDF contents and write them to the output
////        byte[] bytesBuffer = new byte[2048];
////        int bytesRead;
////        while ((bytesRead = pdfInputStream.read(bytesBuffer)) > 0) {
////            responseOutputStream.write(bytesBuffer, 0, bytesRead);
////        }
////         
////        // Make sure that everything is out
////        responseOutputStream.flush();
////          
////        // Close both streams
////        pdfInputStream.close();
////        responseOutputStream.close();
////         
////        // JSF doc:
////        // Signal the JavaServer Faces implementation that the HTTP response for this request has already been generated
////        // (such as an HTTP redirect), and that the request processing lifecycle should be terminated
////        // as soon as the current phase is completed.
////        facesContext.responseComplete();
//        System.out.println("fin");
//        return file;
//    }
}